package com.dameng.jianyouquan.jobhunter.fragment.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.andview.refreshview.XRefreshView;
import com.dameng.jianyouquan.R;
import com.dameng.jianyouquan.adapter.LocalRecycleViewAdapter;
import com.dameng.jianyouquan.jobhunter.activity.JobDetailsActivity;

public class LocalFragment extends Fragment {


    private RecyclerView recyclerView;
    private XRefreshView mXRefreshView;
    private int count = 10;
    private RelativeLayout rl_head3;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getContext(), R.layout.fragment_home_local, null);
        recyclerView = view.findViewById(R.id.recycler_view);

        rl_head3 = view.findViewById(R.id.rl_head3);
        rl_head3.setVisibility(View.INVISIBLE);

        mXRefreshView = view.findViewById(R.id.xRefreshView);
        mXRefreshView.setPullLoadEnable(true);
        mXRefreshView.setPullRefreshEnable(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        LocalRecycleViewAdapter adapter = new LocalRecycleViewAdapter(getActivity());
        recyclerView.setAdapter(adapter);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);


                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();


                Log.e("DDDDDD",firstVisibleItemPosition+"     "+dy);


                if (firstVisibleItemPosition==3&&dy>0){
                    rl_head3.setVisibility(View.VISIBLE);
                }


                if (dy<0&&firstVisibleItemPosition==2){
                    rl_head3.setVisibility(View.INVISIBLE);
                }

            }
        });


        adapter.setOnItemClickListener(v -> startActivity(new Intent(getActivity(), JobDetailsActivity.class)));

        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                mXRefreshView.stopLoadMore();
                adapter.setCount(count += 10);
            }

            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                mXRefreshView.stopRefresh();
                adapter.setCount(10);
            }
        });
//        recyclerView.addItemDecoration(new DividerItemDecoration(inflater.getContext(), DividerItemDecoration.VERTICAL));
        return view;
    }

}
